package rta.datos.conexion
{
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.events.SQLEvent;
	
	import rta.datos.to.Admin;

	public class AdminDB
	{
		private var conn:SQLConnection;
		private var statement:SQLStatement;
		private var _callback:Function;
		
		public function AdminDB(conn:SQLConnection)
		{
			this.conn = conn;
			statement = new SQLStatement();
			statement.itemClass = Admin;
			statement.sqlConnection = conn;
		}
		
		public function set callback(value:Function):void
		{
			_callback = value;
		}
		
		
		//-------------------------------------------------
		
		
		//--LOGIN-----------
		public function login(usuario:String, clave:String):void{					
			statement.text = "SELECT usuario usuario, clave clave " +
							 "FROM administrador " +
							 "WHERE usuario = '" +
							 usuario + 
							 "' AND clave = '" +
							 clave +
							 "'";
			statement.addEventListener(SQLEvent.RESULT, resultLogin);
			statement.execute();
		}
		
		private function resultLogin(event:SQLEvent):void{
			statement.removeEventListener(SQLEvent.RESULT, resultLogin);
			var result:SQLResult = statement.getResult();
			if (result.data != null) {  
				this._callback(true);
			}
			else{
				this._callback(false);
			}
		}
		//-------------------------------------
		
		//--VERIFICAR CONTRASEÑA-----------
		public function verificarClave(clave:String):void{					
			statement.text = "SELECT usuario usuario " +
							 "FROM administrador " +
							 "WHERE clave = @clave ";
			statement.parameters["@clave"] = clave;
			statement.addEventListener(SQLEvent.RESULT, resultVerificarClave);
			statement.execute();
		}
		
		private function resultVerificarClave(event:SQLEvent):void{
			statement.removeEventListener(SQLEvent.RESULT, resultVerificarClave);
			statement.clearParameters();
			var result:SQLResult = statement.getResult();
			if (result.data != null) {  
				this._callback(true);
			}
			else{
				this._callback(false);
			}
		}
		//-------------------------------------
	}
}